﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Threading;
using System.Web;
using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Common.Enums.Base;
using ZBJF.Warranty.Common.Enums.System;
using ZBJF.Warranty.WebProvider;
using WebApiCaller = ZBJF.Warranty.Common.Common.WebApiCaller;

namespace ZBJF.Warranty.H5.Controllers
{
    public class HomeController : BaseController
    {
        public ActionResult Index()
        {
            var employeeRoleIds = ServiceFactory.SellerEmployeeRoleMappingServiceInstance.LoadByEmployeeID(CurrentLoginUser.CurrentUser.ID).Select(o => o.SellerEmployeeRoleID).ToList();
            var isUnionSeller = employeeRoleIds.Contains((byte)SaleSystemTypeEnums.ISPSalingType);
            return RedirectToAction("Index", "InsuranceProduct");
        }
        public ActionResult TestSendMail()
        {
            return View();
        }

        [HttpPost]
        public ActionResult TestSendMail(string email,string orderNo)
        {
            try
            {

                var order = ServiceFactory.InsuranceProductOrderServiceInstance.LoadByOrderNo(orderNo);

                #region 发送邮件
                var mail = new MailHelper();

                var str = WebApiCaller.GetHtmlSourceCode("http://m.zhongbaojinfu.com.cn/InsuranceProductOrder/EmailTemplate/" + order.ID);
                str = str.Replace("src=\"", "src=\"http://m.zhongbaojinfu.com.cn");
                str = str.Replace("url(", "url(http://m.zhongbaojinfu.com.cn");

                var attachmentList = new List<System.Web.Mail.MailAttachment>();

                #region 条款
                var termFile = string.Format("{0}", Server.MapPath("/Attachments/中保金服手机碎屏保障服务合同条款.docx"));
                System.Web.Mail.MailAttachment termData = new System.Web.Mail.MailAttachment(termFile);
                attachmentList.Add(termData);
                #endregion

                #region HTML

                var htmlFile = string.Format("{0}{1}({2}).html", Server.MapPath("/"), order.InsuranceProductName, order.OrderNo);
                System.IO.File.AppendAllText(htmlFile, str);
                System.Web.Mail.MailAttachment data = new System.Web.Mail.MailAttachment(htmlFile);
                attachmentList.Add(data);
                #endregion

                #region PDF
                var bytes = new PechkinPDF().ConvertUrlToBytes("http://m.zhongbaojinfu.com.cn/InsuranceProductOrder/EmailTemplate/" + order.ID, 24);

                var pdfFile = string.Format("{0}{1}({2}).pdf", Server.MapPath("/"), order.InsuranceProductName, order.OrderNo);
                var xx = File(bytes, "application/pdf", pdfFile);
                var yyx = xx.FileContents;
                FileStream fs = new FileStream(pdfFile, FileMode.Create);
                fs.Write(yyx, 0, yyx.Length);
                fs.Close();
                System.Web.Mail.MailAttachment dataPdf = new System.Web.Mail.MailAttachment(pdfFile);
                attachmentList.Add(dataPdf);
                #endregion

                mail.Attachments = attachmentList;

                mail.SendMail(order.BuyUserEmail, "", "您已成功购买服务保障卡，请及时下载卡密，激活服务保障", str);
                Thread.Sleep(10000);
                System.IO.File.Delete(htmlFile);
                System.IO.File.Delete(pdfFile);
                #endregion
            }
            catch (Exception ex)
            {

                throw;
            }
            finally
            {
            }
            return RedirectToAction("TestSendMail");
        }
    }
}